package com.example.springbootdemo.utils;

import com.example.springbootdemo.constant.CommonConst;
import com.example.springbootdemo.entity.GeoPoint;
import com.vividsolutions.jts.geom.Geometry;
import com.vividsolutions.jts.io.ParseException;
import com.vividsolutions.jts.io.WKTReader;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


/**
 * 坐标转换工具 点集合转WTB文本，WTB文本转点集合
 */
@Slf4j
public class GeometryUtil {
    static WKTReader wktReader = new WKTReader();
    //list->geometry
    public static Geometry listToGeometry(List<GeoPoint> geoPoints) throws ParseException {
        if(geoPoints.size()<=0){
            throw new BusinessException(ResultEnum.COORDINATE_NULL);
        }
        String wkt=null;
        int maxindex = geoPoints.size() - 1;

        GeoPoint firstPoint = geoPoints.get(0);
        GeoPoint lastPoint = geoPoints.get(maxindex);
        if (maxindex == 0) { //一个点
             wkt= CommonConst.GEO.GEOMETRY_POINT+"("+firstPoint.getLongitude()+" "+firstPoint.getLatitude()+")";

                return wktReader.read(wkt);

        }
        String stringFotList = getStringFotList(geoPoints);
        if (firstPoint.equals(lastPoint)){ //区域
            wkt=CommonConst.GEO.GEOMETRY_POLYGON+"(("+stringFotList+"))";
        }
        else{//线
            wkt= CommonConst.GEO.GEOMETRY_LINESTRING+"("+stringFotList+")";
        }
        Geometry geometry=null;

        return  geometry= wktReader.read(wkt);


    }

    //geometry->list
    public static List<GeoPoint> geometryToList(Geometry geometry) {
        String geometryString=geometry.toString().replaceAll("\\(|\\)","");
        String s="";
        if(geometryString.startsWith(CommonConst.GEO.GEOMETRY_POINT)) {
           s=geometryString.replace(CommonConst.GEO.GEOMETRY_POINT, "").trim();
        }
        if(geometryString.startsWith(CommonConst.GEO.GEOMETRY_LINESTRING)) {
            s=geometryString.replace(CommonConst.GEO.GEOMETRY_LINESTRING, "").trim();
        }
        if(geometryString.startsWith(CommonConst.GEO.GEOMETRY_POLYGON)) {
            s=geometryString.replace(CommonConst.GEO.GEOMETRY_POLYGON, "").trim();
        }
       return StringToGeoPoints(s);

    }

    private static List<GeoPoint> StringToGeoPoints(String s) {
        List<GeoPoint> geoPoints=new ArrayList<>();
        String[] split = s.split(",");
        for(int i=0;i<split.length;i++){
            String[] s1 = split[i].trim().split(" ");
            geoPoints.add(new GeoPoint(Double.valueOf(s1[0]),Double.valueOf(s1[1])));
        }
        return geoPoints;
    }

    private static String getStringFotList(List<GeoPoint> geoPoints) {
        StringBuilder stringBuilder=new StringBuilder();
        Iterator<GeoPoint> iterator = geoPoints.iterator();
        while (iterator.hasNext()){
            GeoPoint next = iterator.next();
            stringBuilder.append(next.getLongitude()+" "+next.getLatitude()+",");
        }
        String s = stringBuilder.toString();
        return s.substring(0,s.length()-1); //去掉结尾的","
    }

//    public static void main(String[] args) {
//        List<GeoPoint> list=new ArrayList<>();
//       list.add(new GeoPoint(123.12,20.25)) ;
//       list.add(new GeoPoint(123.14,20.25)) ;
//       list.add(new GeoPoint(123.15,20.25)) ;
//       list.add(new GeoPoint(123.16,20.25)) ;
//       list.add(new GeoPoint(123.17,20.25)) ;
//        list.add(new GeoPoint(123.12,20.25)) ;
//        Geometry geometry = null;
//        try {
//            geometry=listToGeometry(list);
//        } catch (ParseException e) {
//            e.printStackTrace();
//        }
//        List<GeoPoint> geoPoints = geometryToList(geometry);
//    }
}
